AddCSLuaFile( "cl_init.lua" );
AddCSLuaFile( "shared.lua" );
include( 'shared.lua' );


function ENT:SpawnFunction( ply, tr )

	local trace = util.GetPlayerTrace( ply, ply:GetAimVector() );
	local tr = util.TraceLine( trace );
	local vending = ents.Create( "ts2_vendingmachine" );
	vending:SetPos( tr.HitPos + tr.HitNormal * 30 );
	vending:Spawn();
	vending:Activate();

end

function ENT:Initialize()

	self.Entity:SetModel( "models/props_interiors/VendingMachineSoda01a.mdl" );
	self.Entity:PhysicsInit( SOLID_VPHYSICS );
	self.Entity:SetMoveType( MOVETYPE_VPHYSICS );
	self.Entity:SetSolid( SOLID_VPHYSICS );

	self.Entity:SetNWInt( "Items", 15 )
	self.Timer = CurTime() + 2.5;

end

function ENT:CreateVendingItem( id, ply )

	--[[local ent = ents.Create( "ts2_item" );
	ent:AttachItem( id );
	ent:SetSolid( 1 );
	ent:Spawn();
	ent:SetPos( self.Entity:GetPos() )
	ent:Activate();--]]
	
	ply:GiveAnyInventoryItem( "soda" );
	ply:SetPlayerMoney( ply:GetPlayerMoney() - 10 );
	
	ply:PrintMessage( 3, "You received soda!" );
	
end	

function ENT:Use( activator, caller )
	
	self.Entity:SetUseType( 3 )
	
	if( self.Entity:GetNWInt( "Items" ) > 0 ) then
	
		if( activator:IsCitizen() ) then
		
			if( activator:GetPlayerMoney() >= 10 ) then
					
				if( not self.Entity:GetNWBool( "Using" ) ) then
					
					self.Entity:SetNWBool( "Using", true );
					self.Entity:EmitSound( "buttons/button4.wav" );
					
					timer.Simple( 2, function() self.Entity:SetNWBool( "Using", false ) end );
			
					self:CreateVendingItem( "sodacola", activator );
					
					self.Entity:SetNWInt( "Items", self.Entity:GetNWInt( "Items" ) - 1 )
						
				end
				
			end	
			
		end	
		
	else
	
		activator:PrintMessage( 3, "This vending machine is out of cans!" )
		self.Entity:SetNWBool( "Using", true )
	
	end	
	
end



	
	